<?php

namespace pc\controllers;
use common\models\StarRanking;

use common\Service\RedisService;
use pc\models\Star;
use pc\models\Tags;
use Yii;
use pc\controllers\BaseController;
use yii\helpers\ArrayHelper;

class BangdanController extends BaseController
{
    public function actionIndex()
    {
        $rankingModel = new StarRanking();
        $needFields = ['id','name','nick_name','header','view']; //查询的字段

        $rankingKeys = ['rk_hot','rk_mainland','rk_taiwan','rk_korea','rk_hybrid','rk_model','rk_girl'];

        //需要排除的明星ID
        $outStarIds = [];

        //获取各榜单数据
        $params = [];
        foreach ($rankingKeys as $rankType){
            $limit = $rankType == 'rk_hot' ? 25 : 6; //默认6条 总榜10条数据
            $params[$rankType] = $rankingModel->pageRanking($rankType,1,$limit,$needFields);
            $outStarIds = array_merge($outStarIds,array_column($params[$rankType]['data'],'id')); //确定需要去重的明星ID
        }
        $outStarIds = array_flip(array_flip($outStarIds));
        //获取随机数据作为站长推荐数据
        $params['randomStars'] = Star::randomStars(5,$needFields,$outStarIds);

        return $this->render('index',$params);
    }

    public function actionList(){
        $type = Yii::$app->request->get('type');
        $page = (int)Yii::$app->request->get('page') ?: 1;

        $rankingType = 'rk_'.$type;
        if(!in_array($rankingType,StarRanking::$rankTypes)){
            //直接404
        }

        $needFields = ['id','name','nick_name','header','view','star_sign','desc']; //查询的字段

        $rankingModel = new StarRanking();
        $params = $rankingModel->pageRanking($rankingType,$page,10,$needFields);

        //需要排除的明星ID
        $outStarIds = array_column($params['data'],'id');

        //获取随机数据作为站长推荐数据
        $params['randomStars'] = Star::randomStars(5,$needFields,$outStarIds);

        $params['navTitle'] = StarRanking::$rankTypeName[$rankingType];

        //设置分页数据
        $pagination = $params['pagination'];
        $pagination['formatUrl'] = '/ranking/'.$type.'/page-{page}.html';
        $pagination['firstUrl'] = '/ranking/'.$type.'/';
        $params['pageString'] = $this->createPageString($pagination);

        $params['page'] = $page;

        return $this->render('info',$params);
    }

    public function actionTest(){
        $model = new StarRanking();
        $data = $model->pageRanking(StarRanking::RANKING_WEEK);
        print_r($data);
    }
}
